package com.me.launcher.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;


import com.me.launcher.bean.AppBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author hejiangwei
 * @date 2017/8/30
 */

public class Tools {
    public static void setViewWidth(View view, int width) {
        view.setVisibility(View.VISIBLE);
        view.getLayoutParams().width = width;
        view.requestLayout();
    }

    public static void setViewHeight(View view, int height) {
        view.setVisibility(View.VISIBLE);
        view.getLayoutParams().height = height;
        view.requestLayout();
    }

    public static void HideInput(View v) {
        InputMethodManager imm = (InputMethodManager) v.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
        }
    }

    public static boolean isHotSeat(String packageName, List<AppBean> appBeans) {
        List<String> arrayList = new ArrayList<>(Arrays.asList("com.android.contacts", "com.android.settings", "com.android.browser", "com.android.camera"));

        boolean result = arrayList.contains(packageName);
        if (!result && appBeans != null && appBeans.size() > 0) {
            boolean isAdded = false;
            for (AppBean appBean : appBeans) {
                if (packageName.equals(appBean.getActivityInfo().packageName)) {
                    isAdded = true;
                    result = false;
                    break;
                }

            }
            //确保底部热键没有插入过，在进行精确匹配
            if (!isAdded) {
                if (packageName.contains(".contacts") || packageName.contains(".settings") || packageName.contains(".browser") || packageName.contains(".camera")) {
                    result = true;
                }

            }

        }

//        Logger.d(packageName+"=="+appBeans.toString());
        return result;
    }

    public static void getRequest(View view) {
        //设置输入框可聚集
        view.setFocusable(true);
        //设置触摸聚焦
        view.setFocusableInTouchMode(true);
        //请求焦点
        view.requestFocus();
        //获取焦点
        view.findFocus();
    }


    /**
     *
     * @param drawable
     * @return
     */
    public static Bitmap drawableToBitmap(Drawable drawable,float scale) {

        Bitmap bitmap = null;
        try {
            bitmap = Bitmap
                    .createBitmap(
                            (int) (drawable.getIntrinsicWidth()*scale),
                            (int) (drawable.getIntrinsicHeight()*scale),
                            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                                    : Bitmap.Config.RGB_565);
            final Canvas canvas = new Canvas(bitmap);
            // canvas.setBitmap(bitmap);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
            drawable.draw(canvas);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
                bitmap = null;
            }
            System.gc();
        }

        return bitmap;
    }
}
